<?php
/* modified: hungtm 8/9/2009 */

/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------string function */
function notNum($value,$msg){ 
	if (!ereg("^[0-9]+$",$value) ){
		echo "$msg must be a number<br>";
		return true;
	}
	return false ;
}


function d4v($value){
	$value = str_replace (array ("'", chr(10),chr(13), '"') , array ('&#39;', '<br/>', '<br/>', '', '&quot;'), trim($value));
	return $value;
}
function d2v($value){
	return trim(htmlspecialchars($value,ENT_QUOTES));
}

function randS($sokytu, $option) {
	$key = "";
	if ($option == "a") {
		$chars1 = array(
			"a","b","c","d","e","f","g","h","i","j","k","l","m",
			"n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3",
			"4","5","6","7","8","9");

		$count1 = count($chars1) - 1;
		srand((double)microtime()*1234567);		
		for($i = 0; $i < (int)$sokytu; $i++)
		{
			$key .= $chars1[rand(0, $count1)];
		}
		return $key;
	}	
	else  if ($option == "0") {
		$chars2 = array("0","1","2","3","4","5","6","7","8","9");
		$count2 = count($chars2) - 1;
		srand((double)microtime()*1234567);		
		for($i = 0; $i < (int)$sokytu; $i++)
		{
			$key .= $chars2[rand(0, $count2)];
		}
		return $key;
	}
	else if ($option == "a0") {
		$chars3 = array(
			"a","b","c","d","e","f","g","h","i","j","k","l","m",
			"n","o","p","q","r","s","t","u","v","w","x","y","z",
			"0","1","2","3","4","5","6","7","8","9");

		$count3 = count($chars3) - 1;
		srand((double)microtime()*1234567);		
		for($i = 0; $i < (int)$sokytu; $i++)
		{
			$key .= $chars3[rand(0, $count3)];
		}
		return $key;
	}
}
function de_post($value){
	$value = str_replace ( array ('SCRIPT', 'javascript', 'checkclick', '<P>', '</P>', "<", ">", '"', "'", '<?', '<?php'), array ('SCRlPT' , 'java_script', '',  '', '<br>' , '{{', '}}', '###', '##', '', ''), $value);
	return $value;
}	
function en_post($value){
	$value = str_replace ( array ('{{', '}}', '###', '##', '[embed]', '[/embed]', '[flash]', '[/flash]', '[url=','/]','[/url]'), 
array ( '<' , '>' , '"', "'", '<object width="280" height="40"><embed src="', '" width="280" height="80"></embed></object>', '<object><embed src="', '" type="application/x-shockwave-flash" width="500"></embed></object>','<a href=','/>','</a>'), $value);
	return $value;
}
function quote_smart($value){
   // Stripslashes
   if (get_magic_quotes_gpc()) {
	   $value = stripslashes($value); 
   }
   // Quote if not integer
   if (!is_numeric($value)) {
	   $value = mysql_real_escape_string($value);
   }
   return $value;
}

/* number function */
function initRand (){
   static $randCalled = FALSE;
   if (!$randCalled)
   {
	   srand((double) microtime() * 1000000);
	   $randCalled = TRUE;
   }
}
function randNum ($low, $high){
   initRand();
   $rNum = rand($low, $high);
   return $rNum;
} 	


/* alert and url function */
function alert($msg){
		echo "<script language='javascript'> alert('".str_replace("\n","\\n",$msg)."');</script>";
} 
function gourl($url){
        if($url==1)
                echo "<script language='javascript'>window.close();</script>";
        else if($url==-1)
                echo "<script language='javascript'>history.go(-1);</script>";
        else
                echo "<script language='javascript'>window.location.href='".$url."';</script>";
				exit;
		exit();
}

function getlink(){
	return $HTTP_SERVER_VARS["HTTP_HOST"];
}
function addlinks($value){
	$value = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\\0\">\\0</a>", $value);
	return $value;
}	
function setGo($gourl){
	$_SESSION['go'] = (string)$gourl;
}

/* ip function */
function getIP() {
	if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
		if ($_SERVER["HTTP_CLIENT_IP"]) {
			$proxy = $_SERVER["HTTP_CLIENT_IP"];
		} else {
			$proxy = $_SERVER["REMOTE_ADDR"];
		}
		$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}
	else {
		if ($_SERVER["HTTP_CLIENT_IP"]) {
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		} else {
			$ip = $_SERVER["REMOTE_ADDR"];
		}
	}
	return trim($ip);
}
function getIP2() {
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
   $ip = getenv("HTTP_CLIENT_IP");

   else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
   $ip = getenv("HTTP_X_FORWARDED_FOR");

   else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
   $ip = getenv("REMOTE_ADDR");

   else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
   $ip = $_SERVER['REMOTE_ADDR'];

   else
   $ip = "unknown";

   return($ip);
} 	

/* language function*/
function getLang() {			
	$l='';
	if($_GET['l']!='') {$l= d2v($_GET['l']); } else {$l= "en"; }
	if (isset($_COOKIE['l'])) {
		$l=$_COOKIE['l'];	
	}	
	else  if (isset($_SESSION['l'])) { 
		$l=$_SESSION['l'];
	}
	return("langs/".$l.".php");	
}			
function addL()	{	// Lay gia tri trong mang cua file langs lay ten nuoc theo ten rut gon
	global $lang;		
	return $lang['cn'];
}
function getL()	{ // Lay gia tri trong mang cua file langs the hien ten nuoc bang so thu tu
	global $lang;		
	return $lang['nc'];
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- database function */
function sql($querry, $option) {
	$dbsql = $querry;
	if ($option==3) {echo "$dbsql"; exit;}
	$dbresult=@mysql_query($dbsql);
	if ($dbresult == null){
		if($option==0) {$viewerr='';} else {$viewerr= "$dbsql<br><br><b>".mysql_error(); }
		die($viewerr);
	 }
	 return $dbresult;
}

function getDBRows($rows, $table, $where, $option) {
	$dbsql = "SELECT ".$rows." FROM ".$table." WHERE 1=1 ".$where;
		if ($option==3) {echo "$dbsql"; exit;}
	$dbresult=@mysql_query($dbsql);
	if ($dbresult == null){
		if($option==0) {$viewerr='';} else {$viewerr= "$dbsql <br><br><b>".mysql_error();}
		die($viewerr);
	}
	if (mysql_num_rows($dbresult) > 0) {
		return mysql_fetch_array($dbresult);
	}
	else {
		return "";
	}
}

function getDBOnefield($fieldname, $table, $where,$option) {
	$dbsql = "SELECT $fieldname FROM $table WHERE 1=1 ".$where;
		if ($option==3) {echo "$dbsql"; exit;}
	$dbresult=@mysql_query($dbsql);
	if ($dbresult == null){
		if($option==0) {$viewerr='';} else {$viewerr= "$dbsql <br><br><b>".mysql_error(); }
		die($viewerr);
	 }
	if (mysql_num_rows($dbresult) > 0) {
		$dbrow = mysql_fetch_array($dbresult);
		return $dbrow[$fieldname];
	}
	else {
		return "";
	}
}



		
function InsertAntiRobotic($randid, $key) {
		//Delete any data on the database for this session ID
		$sql = "DELETE FROM anti_reg WHERE timestamp < '" . (time() - 3600) . "' OR session_id = '" . $randid . "'";

		$result=@mysql_query($sql)
			or die("Error: In Function InsertAntiRobotic()!<br>Error Desc: Could not delete validation key!");

		//Insert registration information
		$sql = "INSERT INTO anti_reg VALUES ('" . $randid . "', '" . $key . "', '" . time() . "')";
		$result=@mysql_query($sql)
			or die("Error: In Function InsertAntiRobotic()!<br>Error Desc: Could not check registration information!");
		return true;
}

/* web data function */
function getWData($url, $t_start, $t_end, $opt) {
	$pos_start = -1;
	$pos_end = -1;
	$gotdata = '';
	$handle = @fopen($url, "rb");
	$data = '';
	if ($handle){
	while (!feof($handle)) {
	  $data .= fread($handle, 8192);
	  if (($pos_start == -1) || ($pos_start === false)) {
			$pos_start = strpos($data, $t_start);
			if (($pos_start > -1) && ($pos_start != false)) {
				$data = substr($data, $pos_start);
			}
	  }
	  elseif (($pos_end == -1) || ($pos_end === false)) {
			$pos_end = strpos($data, $t_end);
			if (($pos_end > -1) && ($pos_end != false)) {
				break;
			}
	  }	
	if (($pos_start > -1) && ($pos_end > -1)) {
		if ($opt == 1) {
				$gotdata = substr($data, 0, $pos_end + strlen($t_end));
		}
		elseif ($opt == 2) {
			$gotdata = substr($data, strlen($t_start), ($pos_end - strlen($t_start)));
		}
	}
	fclose($handle);
	return $gotdata;
		}
	}
	else
		return "";
	
}
function getAllData($url) {
	$handle = @fopen($url, "rb");
	if ($handle){
		$data = '';
		while (!feof($handle)) 
		  $data .= fread($handle, 8192);
		
		fclose($handle);
		return $data;
	}else
		return "";
}

/* other functions */
function remove_table($value){
	$value = preg_replace(array("|<TABLE(.*)>|i","|<font(.*)>|i","|<TD(.*)>|i",'|<TABLE|i', "|<IMG(.*)>|i", "|<IMG|i", "|<br> <br>|i", '|<object(.*)">|i', "|<embed(.*)</embed>|i", '|src=(.*)|i'),
                    array('','','','','','','<br>','','',''), d4v($value));
	if(((strpos('a'.$value, '"'))%2)!=0) {return $value.'"';}
	if(((strpos('a'.$value, "'"))%2)!=0) {return $value."'";}
	if(((strpos('a'.$value, "'"))%2)==0) {return $value;}
}
//function gettext($text,$method,$defaut){
//	if($method=="get") {$value=$_GET['text'];
//
//	isset($value) ? $value : $defaut;}
//	else if ($method=='post'){$value=$_POST['text'];
//alert($value);	isset($value) ? $value : $defaut;}
//	return $value;
//
//}

//----------------------------------------------------------------------------- Funtion about time , data
function notDate($d,$m,$y,$msg){
	$dayOfMonth=array(31,28,31,30,31,31,30,31,30,31,30,31);
	if ( ($d==0) ||
	     ($m==2 && $y%4==0 && $d>29) || // There is 29 days in February of Leap-year
	     (($m!=2 || $y%4!=0) && $d>$dayOfMonth[$m-1]) ) { 	
		echo "$msg (d/m/y) '$d/$m/$y' is invalid<br>";
		return true; 
	} 
	return false;
}

function getTime0()
{
	$chenhGMT = intval(substr(date("O"),0,strlen(date("O"))-2));
	$chenh = 0 - $chenhGMT;
	return time() + $chenh * 3600;
}

function getTimeVN()
{
	$chenhGMT = intval(substr(date("O"),0,strlen(date("O"))-2));
	$chenh = 7 - $chenhGMT;
	return time() + $chenh * 3600;
}
function random_char( $count )
{
$random_char = "";
$char_base = explode( " ", "a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9");

for( $i = 0; $i < $count; $i++ )
{
$radom_char = $radom_char.$char_base[rand(9, count($char_base))];
}

return $radom_char;

} 

function startConfig() {
	session_start();
}
?>